//
//  HQYLawLibraryTypeView.m
//  HONGQIYUN
//
//  Created by leihuiwu on 16/12/22.
//  Copyright © 2016年 Haidi. All rights reserved.
//

#import "HQYLawLibraryTypeView.h"

/// view
#import "HQYSuCollectionView.h"
#import "HQYLineFlowLayout.h"


/// model
#import "LawLibrary.h"


/// item高度
static const CGFloat kItemCellHeight = 40.f;

/// item之间的间隔
static const CGFloat kItemMidlleInteval = 10.f;

/// cell Indentifier
static  NSString *kLawLibraryTypeDetailCellIndentifier = @"kLawLibraryTypeDetailCellIndentifier";
static  NSString *kLawLibraryTypeCellIndentifier = @"kLawLibraryTypeCellIndentifier";



/// 循环滚动 collectionView 高度
static const CGFloat kSuCollectionViewHeight = 95.f;


@interface LawLibraryTypeDetailCell ()

@property (nonatomic, strong) UILabel *lawTypeDetailNameLabel;

@property (nonatomic, copy) NSString *name;

@end


@implementation  LawLibraryTypeDetailCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        [self viewLayout];
    }
    return self;
}

- (void)viewLayout {
    self.layer.cornerRadius = 3;
    self.layer.borderWidth = 0.5f;
    self.layer.borderColor = [UIColor colorFromHexString:@"#DDDDDD"].CGColor;
    self.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:self.lawTypeDetailNameLabel];
    [self.lawTypeDetailNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
        make.width.equalTo(self.mas_width);
        make.height.equalTo(@(self.lawTypeDetailNameLabel.font.lineHeight));
    }];
}

- (void)setName:(NSString *)name {
    if(_name != name) {
        _name = name;
    }
    _lawTypeDetailNameLabel.text = _name;
}

#pragma mark - lazy load
- (UILabel *)lawTypeDetailNameLabel {
    if(!_lawTypeDetailNameLabel) {
        _lawTypeDetailNameLabel = [[UILabel alloc]init];
        _lawTypeDetailNameLabel.font = [UIFont systemFontOfSize:13];
        _lawTypeDetailNameLabel.textColor = [UIColor blackColor];
        _lawTypeDetailNameLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _lawTypeDetailNameLabel;
}



@end


@interface LawLibraryTypeDetailView ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic, strong) UICollectionView *myCollectionView;

@property(nonatomic, strong) UICollectionViewFlowLayout *collectionViewFlowLayout;


@end

@implementation LawLibraryTypeDetailView

- (id)initWithFrame:(CGRect)frame andDataSourceArray:(NSArray *)dataArray {
    
    if(self = [super initWithFrame:frame]) {
        self.dataArray = dataArray;
        [self viewLayout];
    }
    
    return self;
}


- (void)viewLayout {
    [self addSubview:self.myCollectionView];
}

- (void)setDataArray:(NSArray *)dataArray {
    if(_dataArray != dataArray) {
        _dataArray = dataArray;
    }
    
    [self.myCollectionView reloadData];
}

#pragma mark- UICollectionViewDataSource, UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LawLibraryTypeDetailCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kLawLibraryTypeDetailCellIndentifier forIndexPath:indexPath];
    if(indexPath.row < self.dataArray.count) {
        LawLibraryObject *mod = self.dataArray[indexPath.row];
        cell.name = mod.title;
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

#pragma mark- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake( (ScreenWidth - 4 * kItemMidlleInteval) / 3, kItemCellHeight);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 10, 0, 10);
}

- (UICollectionViewFlowLayout *)collectionViewFlowLayout {
    if (!_collectionViewFlowLayout) {
        _collectionViewFlowLayout = [[UICollectionViewFlowLayout alloc] init];
        _collectionViewFlowLayout.minimumLineSpacing = 10;
        _collectionViewFlowLayout.minimumInteritemSpacing = kItemMidlleInteval;
        _collectionViewFlowLayout.headerReferenceSize = CGSizeMake(0, 0);
        _collectionViewFlowLayout.footerReferenceSize = CGSizeMake(0, 0);
        _collectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    }
    return _collectionViewFlowLayout;
}


#pragma mark - lazy load
- (UICollectionView *)myCollectionView {
    if(!_myCollectionView) {
        _myCollectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.collectionViewFlowLayout];
        _myCollectionView.delegate = self;
        _myCollectionView.dataSource = self;
        _myCollectionView.backgroundColor = [UIColor clearColor];
        [_myCollectionView registerClass:[LawLibraryTypeDetailCell class] forCellWithReuseIdentifier:kLawLibraryTypeDetailCellIndentifier];
    }
    return _myCollectionView;
}

@end



@interface LawLibraryTypeCell ()

@property (nonatomic, strong) UILabel *lawTypeNameLabel;

@property (nonatomic, strong) UIImageView *lawTypeIconImageView;

@end

@implementation LawLibraryTypeCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        [self viewLayout];
    }
    return self;
}

- (void)viewLayout {
    
    [self addSubview:self.lawTypeIconImageView];
    [self.lawTypeIconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(10);
        make.width.equalTo(@(50));
        make.height.equalTo(@(50));
        make.centerX.equalTo(self.mas_centerX);
    }];
    
    
    [self addSubview:self.lawTypeNameLabel];
    [self.lawTypeNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).offset(-10);
        make.width.equalTo(self.mas_width);
        make.height.equalTo(@(ceil(self.lawTypeNameLabel.font.lineHeight)));
        make.centerX.equalTo(self.lawTypeIconImageView.mas_centerX);
    }];
    
}

#pragma mark - lazy load
- (UILabel *)lawTypeNameLabel {
    if(!_lawTypeNameLabel) {
        _lawTypeNameLabel = [[UILabel alloc]init];
        _lawTypeNameLabel.font = [UIFont systemFontOfSize:13];
        _lawTypeNameLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _lawTypeNameLabel;
}

- (UIImageView *)lawTypeIconImageView {
    if(!_lawTypeIconImageView) {
        _lawTypeIconImageView = [[UIImageView alloc]init];
    }
    return _lawTypeIconImageView;
}

@end

@interface HQYLawLibraryTypeView () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>

@property (nonatomic, strong) LawLibraryTypeDetailView *lawLibraryTypeDetailView;

@property (nonatomic, strong) UICollectionView *suCollectionView;

@property (nonatomic, strong) HQYLineFlowLayout *lineFlowLayout;

@property (nonatomic, strong) UIImageView *bottomLineImageView;

@property (nonatomic, strong) LawLibrary *lawLibrary;

@property (nonatomic, assign) NSInteger dataArrayCount;

@end

@implementation HQYLawLibraryTypeView

- (id)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]) {
        [self viewLayout];
    }
    return self;
}

- (void)viewLayout {
    
    [self.suCollectionView setFrame:CGRectMake(0, 0, self.width, kSuCollectionViewHeight)];
    [self addSubview:self.suCollectionView];
    
    self.bottomLineImageView.image = [UIImage imageNamed:@"lawLibrary_bottomLine"];
    [self.bottomLineImageView setFrame:CGRectMake(0, self.suCollectionView.bottom + 5, 169.5, 9.f)];
    [self addSubview:self.bottomLineImageView];
    self.bottomLineImageView.centerX = self.width / 2.f;
    
    [self addSubview:self.lawLibraryTypeDetailView];
}

- (void)reloadViewWithModel:(LawLibrary *)lawLibrary {
    self.lawLibrary = lawLibrary;
    
    self.dataArrayCount = self.lawLibrary.lawLibraryObjects.count * 10000;
    [self.suCollectionView reloadData];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.dataArrayCount / 2 inSection:0];
    [self.suCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
    
    NSInteger actualRows = indexPath.row % self.lawLibrary.lawLibraryObjects.count;
    if(actualRows < self.lawLibrary.lawLibraryObjects.count) {
        LawLibraryObject *mod = self.lawLibrary.lawLibraryObjects[actualRows];
        self.lawLibraryTypeDetailView.dataArray = mod.childList;
    }
}

#pragma mark- UICollectionViewDataSource, UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
 //   NSLog(@"======= %zd",indexPath.row);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArrayCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LawLibraryTypeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kLawLibraryTypeCellIndentifier forIndexPath:indexPath];
    
    NSInteger actualRows = indexPath.row % self.lawLibrary.lawLibraryObjects.count;

    if(actualRows < self.lawLibrary.lawLibraryObjects.count) {
        LawLibraryObject *mod = self.lawLibrary.lawLibraryObjects[actualRows];
        [cell.lawTypeIconImageView sd_setImageWithURL:[NSURL URLWithString:mod.img_url] placeholderImage:nil];
        cell.lawTypeNameLabel.text = mod.title;
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    

    [self.suCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    
    NSInteger actualRows = indexPath.row % self.lawLibrary.lawLibraryObjects.count;
    if(actualRows < self.lawLibrary.lawLibraryObjects.count) {
        LawLibraryObject *mod = self.lawLibrary.lawLibraryObjects[actualRows];
        self.lawLibraryTypeDetailView.dataArray = mod.childList;
    }

}

#pragma mark- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(ScreenWidth / 5, kSuCollectionViewHeight);
}

#pragma mark -UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSLog(@"====== %.f",scrollView.contentOffset.x);
    
}

#pragma mark - lazy load
- (HQYLineFlowLayout *)lineFlowLayout {
    if(!_lineFlowLayout) {
        _lineFlowLayout = [[ HQYLineFlowLayout alloc]init];
        _lineFlowLayout.minimumLineSpacing = 0.f;
        _lineFlowLayout.minimumInteritemSpacing = 0.f;
        _lineFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }
    return _lineFlowLayout;
}

- (LawLibraryTypeDetailView *)lawLibraryTypeDetailView {
    if(!_lawLibraryTypeDetailView) {
        _lawLibraryTypeDetailView = [[LawLibraryTypeDetailView alloc]initWithFrame:CGRectMake(0, self.bottomLineImageView.bottom + 5, ScreenWidth, self.height - self.bottomLineImageView.bottom - 5) andDataSourceArray:nil];
    }
    
    return _lawLibraryTypeDetailView;
}

- (UICollectionView *)suCollectionView {
    if(!_suCollectionView) {
        _suCollectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.lineFlowLayout];
        _suCollectionView.delegate = self;
        _suCollectionView.dataSource = self;
        _suCollectionView.backgroundColor = [UIColor clearColor];
        _suCollectionView.showsHorizontalScrollIndicator = NO;
        _suCollectionView.showsVerticalScrollIndicator = NO;
        [_suCollectionView registerClass:[LawLibraryTypeCell class] forCellWithReuseIdentifier:kLawLibraryTypeCellIndentifier];
    }
    return _suCollectionView;
}

- (UIImageView *)bottomLineImageView {
    if(!_bottomLineImageView) {
        _bottomLineImageView = [[UIImageView alloc]init];
    }
    return _bottomLineImageView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
